package com.youqingkuang.ipmsg.ui;

import java.util.ArrayList;

import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.layout.GridData;

public class ConfigWindow extends Dialog{

	private MainWindow mainFrame = null;
	
	public Shell sShell = null;
	public Group group_nickname = null;
	public Group group_groupname = null;
	public Text text_nickname = null;
	public Text text_groupname = null;
	public Group group_sndrcv = null;
	public Button checkBox_checksealing = null;
	public Button checkBox_popmsg = null;
	public Button checkBox_voiceless = null;
	public Button checkBox_sealing = null;
	public Button checkBox_citing = null;
	public Group group_broadcast = null;
	public Label label_broadcast = null;
	public Text text_address = null;
	public Button button_add = null;
	public List list_addresses = null;
	public Button button_remove = null;
	public Button button_OK = null;
	public Button button_cancel = null;
	public Group group_record = null;
	public Button checkBox_record = null;
	public Group group_password = null;
	public Label label1 = null;
	public Text text_old = null;
	public Label label2 = null;
	public Text text_new1 = null;
	public Label label3 = null;
	public Text text_new2 = null;

	
	public ConfigWindow(MainWindow mainFrame){
		super(mainFrame._shell, 0);
		this.mainFrame = mainFrame;
		setText(StringToken.TRAY_CONFIG);
	}
	
	public void open(){
		createSShell();
		loadConfig();
		sShell.open();
	}
	
	public void close(){
		if(!sShell.isDisposed()){
			sShell.close();
		}
	}
	
	/**
	 * This method initializes sShell
	 */
	private void createSShell() {
		GridData gridData7 = new GridData();
		gridData7.heightHint = 30;
		gridData7.horizontalAlignment = GridData.CENTER;
		gridData7.verticalAlignment = GridData.CENTER;
		gridData7.widthHint = 80;
		GridData gridData6 = new GridData();
		gridData6.heightHint = 30;
		gridData6.horizontalAlignment = GridData.CENTER;
		gridData6.verticalAlignment = GridData.CENTER;
		gridData6.widthHint = 80;
		GridLayout gridLayout1 = new GridLayout();
		gridLayout1.numColumns = 2;
		gridLayout1.horizontalSpacing = 10;
		gridLayout1.marginWidth = 9;
		gridLayout1.marginHeight = 8;
		gridLayout1.verticalSpacing = 10;
		sShell = new Shell(SWT.TITLE|SWT.CLOSE);
		sShell.setText(getText());
		sShell.setImage(new Image(Display.getCurrent(), getClass().getResourceAsStream("/com/youqingkuang/ipmsg/ui/icon.png")));
		sShell.setLayout(gridLayout1);
		createGroup_nickname();
		createGroup_groupname();
		sShell.setSize(new Point(336, 426));
		createGroup_sndrcv();
		createGroup_record();
		createGroup_password();
		createGroup_broadcast();
		button_OK = new Button(sShell, SWT.NONE);
		button_OK.setText(StringToken.OK);
		button_OK.setLayoutData(gridData6);
		button_OK.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				saveConfig();
				mainFrame._socketTool.setNickName(Config.getInstance().nickname);
				mainFrame._socketTool.setGroupName(Config.getInstance().groupname);
				close();
			}
		});
		button_cancel = new Button(sShell, SWT.NONE);
		button_cancel.setText(StringToken.CANCEL);
		button_cancel.setLayoutData(gridData7);
		button_cancel.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				close();
			}
		});
		
	}

	/**
	 * This method initializes group_nickname	
	 *
	 */
	private void createGroup_nickname() {
		GridData gridData2 = new GridData();
		gridData2.horizontalAlignment = GridData.BEGINNING;
		gridData2.heightHint = 35;
		gridData2.widthHint = 120;
		gridData2.verticalAlignment = GridData.CENTER;
		group_nickname = new Group(sShell, SWT.NONE);
		group_nickname.setText(StringToken.USER_NAME);
		group_nickname.setLayoutData(gridData2);
		FillLayout layout = new FillLayout();
		layout.marginHeight = 5;
		layout.marginWidth = 5;
		group_nickname.setLayout(layout);
		text_nickname = new Text(group_nickname, SWT.BORDER);
	}

	/**
	 * This method initializes group_groupname	
	 *
	 */
	private void createGroup_groupname() {
		GridData gridData3 = new GridData();
		gridData3.horizontalAlignment = GridData.END;
		gridData3.grabExcessHorizontalSpace = false;
		gridData3.grabExcessVerticalSpace = false;
		gridData3.heightHint = 35;
		gridData3.widthHint = 120;
		gridData3.verticalAlignment = GridData.CENTER;
		group_groupname = new Group(sShell, SWT.NONE);
		FillLayout layout = new FillLayout();
		layout.marginHeight = 5;
		layout.marginWidth = 5;
		group_groupname.setLayout(layout);
		group_groupname.setLayoutData(gridData3);
		group_groupname.setText(StringToken.GROUP_NAME);
		text_groupname = new Text(group_groupname, SWT.BORDER);
	}

	/**
	 * This method initializes group_sndrcv	
	 *
	 */
	private void createGroup_sndrcv() {
		GridData gridData1 = new GridData();
		gridData1.verticalSpan = 2;
		gridData1.horizontalAlignment = GridData.BEGINNING;
		gridData1.verticalAlignment = GridData.FILL;
		group_sndrcv = new Group(sShell, SWT.NONE);
		group_sndrcv.setText(StringToken.CONFIG_GROUP_SNDRCV);
		group_sndrcv.setLayoutData(gridData1);
		FillLayout layout = new FillLayout(SWT.VERTICAL);
		layout.marginHeight = 5;
		layout.marginWidth = 5;
		group_sndrcv.setLayout(layout);
		checkBox_checksealing = new Button(group_sndrcv, SWT.CHECK);
		checkBox_checksealing.setText(StringToken.CONFIG_CHECKSEALING);
		checkBox_popmsg = new Button(group_sndrcv, SWT.CHECK);
		checkBox_popmsg.setText(StringToken.CONFIG_POPMSG);
		checkBox_voiceless = new Button(group_sndrcv, SWT.CHECK);
		checkBox_voiceless.setText(StringToken.CONFIG_VOICELESS);
		checkBox_sealing = new Button(group_sndrcv, SWT.CHECK);
		checkBox_sealing.setText(StringToken.CONFIG_SEALING);
		checkBox_citing = new Button(group_sndrcv, SWT.CHECK);
		checkBox_citing.setText(StringToken.CONFIG_CITING);
	}

	/**
	 * This method initializes group_broadcast	
	 *
	 */
	private void createGroup_broadcast() {
		GridData gridData = new GridData();
		gridData.horizontalSpan = 2;
		gridData.verticalAlignment = GridData.FILL;
		gridData.horizontalAlignment = GridData.FILL;
		group_broadcast = new Group(sShell, SWT.NONE);
		group_broadcast.setLayout(null);
		group_broadcast.setLayoutData(gridData);
		group_broadcast.setText(StringToken.CONFIG_GROUP_BROADCAST);
		label_broadcast = new Label(group_broadcast, SWT.NONE);
		label_broadcast.setText(StringToken.CONFIG_LBL_BROADCAST);
		label_broadcast.setBounds(new Rectangle(7, 20, 120, 12));
		text_address = new Text(group_broadcast, SWT.BORDER);
		text_address.setBounds(new Rectangle(8, 42, 119, 26));
		list_addresses = new List(group_broadcast, SWT.NONE);
		list_addresses.setBounds(new Rectangle(169, 16, 130, 76));
		button_add = new Button(group_broadcast, SWT.NONE);
		button_add.setBounds(new Rectangle(134, 29, 23, 22));
		button_add.setText(StringToken.CONFIG_ADD_ADDRESS);
		button_add.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if(text_address.getText().length()>0)
					list_addresses.add(text_address.getText());
			}
		});
		button_remove = new Button(group_broadcast, SWT.NONE);
		button_remove.setBounds(new Rectangle(134, 58, 23, 22));
		button_remove.setText(StringToken.CONFIG_REMOVE_ADDRESS);
		button_remove.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				int index = list_addresses.getSelectionIndex();
				if(index!=-1){
					list_addresses.remove(index);
				}
			}
		});
	}

	/**
	 * This method initializes group_record	
	 *
	 */
	private void createGroup_record() {
		GridData gridData4 = new GridData();
		gridData4.horizontalAlignment = GridData.FILL;
		gridData4.verticalAlignment = GridData.CENTER;
		group_record = new Group(sShell, SWT.NONE);
		FillLayout layout = new FillLayout(SWT.VERTICAL);
		layout.marginHeight = 5;
		layout.marginWidth = 5;
		group_record.setLayout(layout);
		group_record.setLayoutData(gridData4);
		group_record.setText(StringToken.CONFIG_GROUP_RECORD);
		checkBox_record = new Button(group_record, SWT.CHECK);
		checkBox_record.setText(StringToken.CONFIG_RECORD);
	}

	/**
	 * This method initializes group_password	
	 *
	 */
	private void createGroup_password() {
		GridData gridData10 = new GridData();
		gridData10.horizontalAlignment = GridData.FILL;
		gridData10.verticalAlignment = GridData.CENTER;
		GridData gridData9 = new GridData();
		gridData9.horizontalAlignment = GridData.FILL;
		gridData9.verticalAlignment = GridData.CENTER;
		GridData gridData8 = new GridData();
		gridData8.grabExcessHorizontalSpace = true;
		gridData8.verticalAlignment = GridData.CENTER;
		gridData8.horizontalAlignment = GridData.FILL;
		GridData gridData5 = new GridData();
		gridData5.horizontalAlignment = GridData.FILL;
		gridData5.verticalAlignment = GridData.CENTER;
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		group_password = new Group(sShell, SWT.NONE);
		group_password.setText(StringToken.CONFIG_MODIFY_PASSWORD);
		group_password.setLayoutData(gridData5);
		group_password.setLayout(gridLayout);
		label1 = new Label(group_password, SWT.NONE);
		label1.setText(StringToken.CONFIG_OLD_PASSWORD);
		text_old = new Text(group_password, SWT.BORDER);
		text_old.setLayoutData(gridData8);
		label2 = new Label(group_password, SWT.NONE);
		label2.setText(StringToken.CONFIG_NEW_PASSWORD);
		text_new1 = new Text(group_password, SWT.BORDER);
		text_new1.setLayoutData(gridData9);
		label3 = new Label(group_password, SWT.NONE);
		label3.setText(StringToken.CONFIG_CONFIRM_PASSWORD);
		text_new2 = new Text(group_password, SWT.BORDER);
		text_new2.setLayoutData(gridData10);
	}

	private void loadConfig(){
		text_nickname.setText(Config.getInstance().nickname);
		text_groupname.setText(Config.getInstance().groupname);
		if(Config.getInstance().addresses!=null)
			for(String address : Config.getInstance().addresses)
				list_addresses.add(address);
		checkBox_checksealing.setSelection(Config.getInstance().checkSealing) ;
		checkBox_popmsg.setSelection(Config.getInstance().popMsg);
		checkBox_voiceless.setSelection(Config.getInstance().voiceless);
		checkBox_sealing.setSelection(Config.getInstance().sealing);
		checkBox_citing.setSelection(Config.getInstance().citing);
		checkBox_record.setSelection(Config.getInstance().record);
	}
	
	private void saveConfig(){
		Config.getInstance().nickname = text_nickname.getText();
		Config.getInstance().groupname = text_groupname.getText();
		if(Config.getInstance().addresses!=null){
			Config.getInstance().addresses.clear();
		}
		if(list_addresses.getItemCount()>0){
			if(Config.getInstance().addresses==null){
				Config.getInstance().addresses=new ArrayList<String>();
			}
			for(String address:list_addresses.getItems())
				Config.getInstance().addresses.add(address);
		}
		if(Config.getInstance().password.equals(text_old.getText())){
			if(text_new1.getText().equals(text_new2.getText())){
				Config.getInstance().password=text_new1.getText();
			}
		}else{
			MessageBox dialog = new MessageBox(sShell, SWT.OK
					| SWT.ICON_INFORMATION);
			dialog.setMessage(StringToken.CONFIG_PASSWORD_ERROR);
			dialog.open();
		}
		
		Config.getInstance().checkSealing=checkBox_checksealing.getSelection();
		Config.getInstance().popMsg=checkBox_popmsg.getSelection();
		Config.getInstance().voiceless=checkBox_voiceless.getSelection();
		Config.getInstance().sealing=checkBox_sealing.getSelection();
		Config.getInstance().citing=checkBox_citing.getSelection();
		Config.getInstance().record=checkBox_record.getSelection();
		Config.getInstance().save();
	}
}
